package models

import (
	"cargo-hub/appconfig"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var (
	DB  *gorm.DB
	err error
)

func Init() {
	my := appconfig.Config.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		my.User,
		my.Password,
		my.Host,
		my.Port,
		my.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据库连接成功")
	err = DB.AutoMigrate(&User{}, &UserRole{}, &Role{}, &RoleNode{}, &Node{}, &Warehouse{}, &Order{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据表迁移成功")
}
